#! /usr/bin/env python

# -*- coding: UTF-8 -*-
# ==============================================================================
# Filename : ReceiveCMD.py
# Purpose  : design for jayson to run multiple given commands and summarize to a csv file
# Author   : Yohann Li (lyh001686@sina.com)
# Date     : 2020-09-04 16:06
# ==============================================================================
import sys

from PyQt5.QtCore import pyqtSignal, Qt, QPoint
from PyQt5.QtWidgets import QWidget, QApplication, QTextEdit, QPushButton, QGridLayout, QDialogButtonBox


class ReceiceCMDEdit(QWidget):
    text_signal = pyqtSignal(str)

    def __init__(self, *args, **kwargs):
        super(ReceiceCMDEdit, self).__init__(*args, **kwargs)
        self.setWindowFlag(Qt.FramelessWindowHint)
        self.setWindowOpacity(0.9)
        self.resize(500, 700)
        self.move(int((QApplication.desktop().screen().width() - self.width()) / 2),
                  int((QApplication.desktop().screen().height() - self.height()) / 3))
        self._init_ui()

    def _init_ui(self):
        self.edit = QTextEdit()
        self.edit.setLineWrapMode(QTextEdit.NoWrap)
        self.btn_ok = QPushButton('OK')
        self.btn_ok.clicked.connect(self.ok_clicked)
        self.btn_cancel = QPushButton('Cancel')
        self.btn_cancel.clicked.connect(self.close)
        self.btn_box = QDialogButtonBox()
        self.btn_box.addButton(self.btn_cancel, QDialogButtonBox.RejectRole)
        self.btn_box.addButton(self.btn_ok, QDialogButtonBox.AcceptRole)
        lay = QGridLayout(self)
        lay.addWidget(self.edit, 1, 1, 1, 5)
        lay.addWidget(self.btn_box, 2, 4, 1, 2)

    def ok_clicked(self):
        self.text_signal.emit(self.edit.toPlainText())
        self.close()

    def mousePressEvent(self, event):
        self.currentPos = event.pos()

    def mouseMoveEvent(self, event):
        self.move(QPoint(self.pos() + event.pos() - self.currentPos))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setCursorFlashTime(0)
    t = ReceiceCMDEdit()
    t.show()
    sys.exit(app.exec_())
